package com.example.micro_property_springboot.controller;

import com.example.micro_property_springboot.pojo.vo.OperLogQueryVO;
import com.example.micro_property_springboot.service.SysOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 操作日志控制器
 * 提供操作日志相关的REST API接口
 */
@RestController
@RequestMapping("/api/system/oper-log")
public class SysOperLogController {
    
    @Autowired
    private SysOperLogService operLogService;
    
    /**
     * 分页查询操作日志
     */
    @GetMapping("/page")
    public ResponseEntity<Map<String, Object>> queryOperLogWithPage(
            OperLogQueryVO query,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Map<String, Object> result = operLogService.queryOperLogWithPage(query, pageNum, pageSize);
        return ResponseEntity.ok(result);
    }
    
    /**
     * 根据ID删除操作日志
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Boolean> deleteOperLog(@PathVariable Long id) {
        boolean success = operLogService.deleteOperLog(id);
        return ResponseEntity.ok(success);
    }
    
    /**
     * 批量删除操作日志
     */
    @DeleteMapping("/batch")
    public ResponseEntity<Boolean> batchDeleteOperLogs(@RequestBody List<Long> ids) {
        boolean success = operLogService.batchDeleteOperLogs(ids);
        return ResponseEntity.ok(success);
    }
    
    /**
     * 清空操作日志
     */
    @DeleteMapping("/clear")
    public ResponseEntity<Boolean> clearOperLogs() {
        boolean success = operLogService.clearOperLogs();
        return ResponseEntity.ok(success);
    }
    
    /**
     * 删除指定时间之前的操作日志
     */
    @DeleteMapping("/old")
    public ResponseEntity<Boolean> deleteOldOperLogs(@RequestParam String date) {
        boolean success = operLogService.deleteOldOperLogs(date);
        return ResponseEntity.ok(success);
    }
    
    /**
     * 获取操作日志统计信息
     */
    @GetMapping("/statistics")
    public ResponseEntity<Map<String, Object>> getOperLogStatistics() {
        Map<String, Object> statistics = operLogService.getOperLogStatistics();
        return ResponseEntity.ok(statistics);
    }
    
    /**
     * 获取操作类型统计
     */
    @GetMapping("/statistics/oper-type")
    public ResponseEntity<List<Map<String, Object>>> getOperTypeStatistics() {
        List<Map<String, Object>> statistics = operLogService.getOperTypeStatistics();
        return ResponseEntity.ok(statistics);
    }
    
    /**
     * 获取模块访问统计
     */
    @GetMapping("/statistics/module")
    public ResponseEntity<List<Map<String, Object>>> getModuleAccessStatistics() {
        List<Map<String, Object>> statistics = operLogService.getModuleAccessStatistics();
        return ResponseEntity.ok(statistics);
    }
}